package com.mall.huohuo.service.member;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.huohuo.datasource.entity.TblMemberAddress;
import com.mall.huohuo.datasource.mapper.TblMemberAddressMapper;
import com.mall.huohuo.result.PageResult;
import com.mall.huohuo.service.address.domain.MemberAddress;
import com.mall.huohuo.service.address.service.MemberAddressInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyconding
 * @version AuthParam.java, v 0.1 2020-08-07 16:23 zyconding
 */
@RequiredArgsConstructor
@Service
public class AddressServiceImpl implements MemberAddressInfoService {
    @Autowired
    private TblMemberAddressMapper tblMemberAddressMapper;

//    @Autowired
//    private MemberInfoService memberInfoService;
    /**
     * 获取用户地址
     *
     * @param memberId 用户id
     * @param pageNum  当前页页码
     * @param pageSize 每页大小
     * @return 分页之后的用户地址，默认是按照默认的位置来返回
     */
    @Override
    public PageResult<MemberAddress> getMemberAddressList(Long memberId, Integer pageNum, Integer pageSize) {
        assertMemberExist(memberId);

        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<TblMemberAddress> objectLambdaQueryWrapper = Wrappers.lambdaQuery();
        objectLambdaQueryWrapper.eq(TblMemberAddress::getMemberId, memberId);
        List<TblMemberAddress> tblMemberAddresses = tblMemberAddressMapper.selectList(objectLambdaQueryWrapper);
        // 这边会有一个转换的方法
        final PageInfo<TblMemberAddress> pageInfo = new PageInfo<>(tblMemberAddresses);
        PageResult<TblMemberAddress> tblMemberAddressPageResult = PageResult.convertPageInfo(pageInfo);
        PageResult<MemberAddress> pageResult = new PageResult<MemberAddress>();
        pageResult.setSuccess(tblMemberAddressPageResult.getSuccess());
        if (!tblMemberAddressPageResult.getSuccess()) {
            return pageResult;
        }
        final List<MemberAddress> memberAddressList =
                JSONObject.parseObject(JSONObject.toJSONString(tblMemberAddresses), new TypeReference<List<MemberAddress>>() {
        });
        BeanUtils.copyProperties(pageInfo, pageResult, "data");
        pageResult.setData(memberAddressList.stream().sorted().collect(Collectors.toList()));
        return pageResult;
    }

    /**
     * 获取用户地址信息
     *
     * @param memberId  用户id
     * @param addressId 地址id
     * @return 用户收货地址
     */
    @Override
    public MemberAddress getMemberAddressDetail(Long memberId, Long addressId) {
        assertMemberExist(memberId);
        TblMemberAddress tblMemberAddress = tblMemberAddressMapper.selectOne(Wrappers.<TblMemberAddress>lambdaQuery()
                .eq(TblMemberAddress::getId, addressId).eq(TblMemberAddress::getMemberId, memberId).eq(TblMemberAddress::getIsDeleted, false));
        Assert.notNull(tblMemberAddress, "用户收货地址不存在。");
        MemberAddress memberAddress = new MemberAddress();
        BeanUtils.copyProperties(tblMemberAddress,memberAddress);
        return memberAddress;
    }



    private void assertMemberExist(Long memerId) {

    }
}
